<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h2>对象深拷贝实现</h2>
</body>
<script>
    
    const obj1={
        name:'jack',
        age:18,
        fn:function() {},
        aaa:undefined,
        info:{
            address:'北京',
            hobby:['运动','看电影','看书']
        }
    }

   // const obj2=JSON.parse(JSON.stringify(obj1))
    //obj2.info.address='深圳'
    //console.log(obj1)
   // console.log(obj2)

   function cloneDeep(target) {
         //如果不是object,直接返回
        if(typeof target!=='object') return target;
        //定义一个初值
       let result=Array.isArray(target) ? []:{}

        //遍历递归调用
        for(let key in target) {
            result[key]=typeof target[key]==='object' ? cloneDeep(target[key]) : target[key]
        }


        return result;
    

   }

  const obj2=cloneDeep(obj1)
  obj2.info.hobby.push('游泳1111')
  console.log(obj1)
  console.log(obj2)




   
</script>
</html>